//
//  FYStatusPictureView.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/13.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit
import SDWebImage

private let StatusPictureViewCellIdentifier = "StatusPictureViewCellIdentfier"

private let itemMargin: CGFloat = 5

private let itemWidth = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin) / 3

class FYStatusPictureView: UICollectionView {

    //  显示配图个数
    private lazy var messageLabel: UILabel = UILabel(fontSize: 20, textColor: UIColor.whiteColor())
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //  垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //  显示配图的数据源
    var picUrls: [FYStatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            
            //  根据配图的个数计算大小
            let size = calcSizeWithCount(picUrls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            self.reloadData()
        }
    }
    
    private func setupUI() {
        
        //  注册cell
        registerClass(FYStatusPictureViewCell.self, forCellWithReuseIdentifier: StatusPictureViewCellIdentifier)
        
        dataSource = self
        
//        addSubview(messageLabel)
//        
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
        
    }
    
    private func calcSizeWithCount(count: Int) -> CGSize {
        //  列数怎么计算
        //  列数
        let cols = count > 3 ? 3 : count
        //  行数
        let rows = (count - 1) / 3 + 1
        //  计算配图大小
        //  宽度
        let currentWidth = itemWidth * CGFloat(cols) + CGFloat(cols - 1) * itemMargin
        //  高度
        let currentHeight = itemWidth * CGFloat(rows) + CGFloat(cols - 1) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
        
    }
    
}

extension FYStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureViewCellIdentifier, forIndexPath: indexPath) as! FYStatusPictureViewCell
        //  设置模型数据
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
}

//  自定义配图cell
class FYStatusPictureViewCell: UICollectionViewCell {
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //  修改显示模型为等比填充,保证图片比例不变
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //  多余图片剪切掉
        view.clipsToBounds = true
        
        return view
    }()
    
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    //  绑定数据
    var picInfo: FYStatusPictureInfo? {
        didSet {
            if let url = picInfo?.thumbnail_pic {
                imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage:UIImage(named: "timeline_image_placeholder"))
                
              gifImageView.hidden = !url.hasSuffix(".gif")
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  添加控件
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        
        //  设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
}


